package tuiLayer;
import controlLayer.*;
import modelLayer.*;

import java.util.Scanner;


public class StaffUI
{
    // instance variables - replace the example below with your own
    Scanner keyboard = new Scanner(System.in);
    String name, phoneNo, username, password;
    int cprNo;
    boolean isManager = false;
    private String message="";
    StaffController sc = new StaffController();
    String errorMessage = "Incorrect command";
    MainUI mainUI;
    

    /**
     * Constructor for objects of class EmployeeUI
     */
    public StaffUI()
    {
        // initialise instance variables
        writeStaffMenu();
    }

    public void writeStaffMenu () {
      
        //getString();
        System.out.println("\f***Staff Menu***");
        System.out.println(" (1) Add Staff");
        System.out.println(" (2) Update Staff");
        System.out.println(" (3) Get staff list");
        System.out.println(" (4) Staff information");
       // System.out.println("* (5) Remove staff()");
        System.out.println(" (6) Back");
        String choice = getString();
        switch (choice) {
            case "1":
                addStaff();
                writeStaffMenu();
                break;
            case "2":
                updateStaff();
                writeStaffMenu();
                break;
            case "3":
                getStaffList();
                writeStaffMenu();
                break;
            case "4":
                showStaffInfo();
                writeStaffMenu();
                break;
                /*
            case "5":
                removeStaff();
                writeStaffMenu();
                break;
                */
            case "6":
                mainUI = new MainUI();
                mainUI.writeMainMenu();
                break;
            default:
                System.out.println(errorMessage);
                pressEnter();
                writeStaffMenu();
        }
    }
    
    private void showStaffInfo(){
       String username;
       System.out.println("Type username: ");
       username=getString();
       sc.printStaffInfo(username);
       pressEnter();
    }
    
    private void addStaff() {
        String name, phoneNo, username, password;
        int cprNo=0;
        boolean isManager=false;
        boolean unic;
        boolean found = false;
        System.out.println("Type the name: ");
        name=getString();
        System.out.println("Type the phone number: ");
        phoneNo=getString();
        System.out.println("Type the CPR number: ");
        cprNo = Integer.parseInt(getString());
        System.out.println("Manager or Employee?");
        String job = getString();
        while (!job.equals("manager") && !job.equals("employee")) {
          System.out.println("I don't know what you mean! Try again!");
          job = getString();
        }
           if (job == "manager") {
          isManager=true;
          }
          else if (job == "employee") {
            isManager=false;
          }
          
        
        do {
            unic =true;
            Staff s=null;
            System.out.println("Create username: ");
            username=getString();
            s=sc.findStaffByUserName(username);
            if(s!=null) {
                System.out.println("The username is already in the system. Create a new one!");
                unic=false;
            }
        }while(!unic);
        System.out.println("Create a password: ");
        password=getString();
        Staff staff =  new Staff(name, phoneNo, cprNo, username, password, isManager);
        sc.addStaff(staff);
        System.out.println("Staff member created.");
        pressEnter();
    }
    
    private void updateStaff() {
        Staff s=null;
        String username="";
        String name="";
        String phoneNo="";
        String password="";
        
        int cprNo=0;
        boolean isManager=false;
        String job="";
        System.out.println("Enter username: ");
        username=getString();
        s=sc.findStaffByUserName(username);
        
        if (s==null) {
            writeStaffMenu();
        }
        else {
            name = s.getName();
            phoneNo = s.getPhoneNo();
            password = s.getPassword();
            cprNo = s.getCprNo();
            isManager = s.getIsManager();
        }
        boolean done=false;
        while (!done && s!=null) {
            System.out.println("\f***Update staff menu***");
            System.out.println("(1)Update name");
            System.out.println("(2)Update phone number");
            System.out.println("(3)Update CPR number");
            System.out.println("(4)Update username");
            System.out.println("(5)Update position");
            System.out.println("(6)Update password");
            System.out.println("(7)Update and go back");
            System.out.println("(8)Back");
            System.out.println("\nType command: ");
            //done=true;

        
          String choice = getString();
          switch(choice){
            
            case "1":
                    System.out.println("Type name: ");
                    name=getString();
                    break;
            case "2":
                    System.out.println("Type phone number: ");
                    phoneNo=getString();
                    break;
            case "3":
                    System.out.println("Type CPR number: ");
                    cprNo=Integer.parseInt(getString());
                    break;
            case "4":
                    System.out.println("Type username: ");
                    username=getString();
                    break;
            case "5":
                    System.out.println("Type position: ");
                    job=getString();
                    if (job == "manager") {
                        isManager=true;
                    }
                    else if (job == "employee") {
                        isManager=false;
                    }
                    break;
            case "6":
                    boolean unic;
                    do{
                    unic=true;
                    System.out.println("Type the password: ");
                    password=getString();
                    for(Staff staff: sc.getAllStaff()){
                        if(staff.getPassword().equals(password))
                        unic=false;            
                    }
                    if(unic==false) 
                    System.out.println("password already exists. Try again\nTry again");
                   }while(!unic);
                    break;
            case "7":
                    sc.updateStaff(s, name, phoneNo, cprNo, username, password, isManager);            
                    done=true;
                    System.out.println("Staff updated!");
                    pressEnter();
                    writeStaffMenu();
                    break;
            case "8":
                    writeStaffMenu();
                    break;
            default:
                    System.out.println(errorMessage);
                    pressEnter();
                    updateStaff();
                }  
            }
    }
    
    private void getStaffList() {
        System.out.println("Staff list: ");
        sc.printAllStaffInfo();
        pressEnter();
    }
    
    private void removeStaff() {
        String username="";
        Staff staff=null;
        System.out.println("Type username in order to remove: ");
        username=getString();
        staff=sc.getStaff(username);
        System.out.println("Would you like to remove a staff member? Yes(1) or No(2).");
        String choice = getString();
        switch (choice) {
            case "1":
                if (sc.removeStaff(staff)) {
                    System.out.println("Staff member removed");
                }
                else {
                    System.out.println("Staff member doesn't exist in the database");
                }
                pressEnter();
                break;
            case "2":
                writeStaffMenu();
                break;
            default:
                System.out.println(errorMessage);
                pressEnter();
                removeStaff();
        }
    }
    
   
    
    private String getString() {
        String startChoice="";
        startChoice = keyboard.next();
        String choice = startChoice.trim().toLowerCase();
        return choice;
    }
    
    private void pressEnter() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Press enter to continue");
        scanner.nextLine();
    }
    
}
